﻿using CoachingOrganizer.WinPhone.Common;
using CoachingOrganizer.WinPhone.UITools;
using CoachingOrganizer.WinPhone.ViewModels.Tools.EmotionsPack;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Display;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556

namespace CoachingOrganizer.WinPhone.Pages.Tools.EmotionsPack
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class EmotionsPackPage : Page
    {
        private NavigationDecorator _navigationDecorator;

        public EmotionsPackPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            EmotionsPackViewModel viewModel = UIHelper.GetDataContext<EmotionsPackViewModel>(e);
            DataContext = viewModel;
            BeginTraceEmotionPreview(viewModel);
            _navigationDecorator = new NavigationDecorator(this);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            EmotionsPackViewModel viewModel = DataContext as EmotionsPackViewModel;
            if (viewModel != null)
                EndTraceEmotionPreview(viewModel);
        }

        private async void BeginTraceEmotionPreview(EmotionsPackViewModel viewModel)
        {
            await viewModel.InitTraceEmotionPreview();
            _traceEmotionCaptureElement.Source = viewModel.TraceEmotionSource;
            await viewModel.BeginTraceEmotionPreview();
        }

        private async void EndTraceEmotionPreview(EmotionsPackViewModel viewModel)
        {
            await viewModel.EndTraceEmotionPreview();
        }
    }
}
